package com.tsystems.train.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;

@Entity
public class TimeTableElement implements Serializable {

	@Id
	private String id;

	@PrePersist
	public void prepareId() {
		if (id == null) {
			id = UUID.randomUUID().toString();
		}
	}

	private static final long serialVersionUID = -994487055812091568L;

	@NotNull(message = "Train is required")
	@ManyToOne
	private Train train;

	@NotNull(message = "Departure station is required")
	@ManyToOne
	private Station departureStation;

	@NotNull(message = "Arrival station is required")
	@ManyToOne
	private Station arrivalStation;

	@NotNull(message = "Departure time is required.")
	@Future(message = "Departure time must be in future")
	private Date departureTime;

	@NotNull(message = "Arrival time is required.")
	@Future(message = "Arrival time must be in future")
	private Date arrivalTime;

	@OneToMany(mappedBy = "timeTableElement")
	private List<Ticket> tickets;

	public TimeTableElement() {
		this.tickets = new ArrayList<Ticket>();
	}

	public TimeTableElement(Train train, Station departureStation,
			Station arrivalStation, Date departureTime, Date arrivalTime) {
		this();
		this.train = train;
		this.departureStation = departureStation;
		this.arrivalStation = arrivalStation;
		this.departureTime = departureTime;
		this.arrivalTime = arrivalTime;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Station getDepartureStation() {
		return departureStation;
	}

	public void setDepartureStation(Station departureStation) {
		this.departureStation = departureStation;
	}

	public Station getArrivalStation() {
		return arrivalStation;
	}

	public void setArrivalStation(Station arrivalStation) {
		this.arrivalStation = arrivalStation;
	}

	public Date getDepartureTime() {
		return departureTime;
	}

	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}

	public Date getArrivalTime() {
		return arrivalTime;
	}

	public void setArrivalTime(Date arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	public List<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	@Override
	public String toString() {

		return train.toString() + "(" + departureStation.getName() + ", "
				+ arrivalStation.getName() + ")";
	}

	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((arrivalStation == null) ? 0 : arrivalStation.hashCode());
		result = prime * result
				+ ((arrivalTime == null) ? 0 : arrivalTime.hashCode());
		result = prime
				* result
				+ ((departureStation == null) ? 0 : departureStation.hashCode());
		result = prime * result
				+ ((departureTime == null) ? 0 : departureTime.hashCode());
		result = prime * result + ((train == null) ? 0 : train.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TimeTableElement other = (TimeTableElement) obj;
		if (arrivalStation == null) {
			if (other.arrivalStation != null)
				return false;
		} else if (!arrivalStation.equals(other.arrivalStation))
			return false;
		if (arrivalTime == null) {
			if (other.arrivalTime != null)
				return false;
		} else if (!arrivalTime.equals(other.arrivalTime))
			return false;
		if (departureStation == null) {
			if (other.departureStation != null)
				return false;
		} else if (!departureStation.equals(other.departureStation))
			return false;
		if (departureTime == null) {
			if (other.departureTime != null)
				return false;
		} else if (!departureTime.equals(other.departureTime))
			return false;
		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train))
			return false;
		return true;
	}

	public String getId() {
		return id;
	}

}
